package com.captain.special.二叉树.简单;

import com.captain.leetcode.TreeNode;

/**
 * Des:
 *
 * @author XL
 * @Date 2022/5/6 20:51
 */
public class 平衡二叉树110 {
    public boolean isBalanced(TreeNode root) {
        return recur(root) != -1;
    }

    public int recur(TreeNode node) {
        if (node == null) {
            return 0;
        }
        int leftVal = recur(node.left);
        if (leftVal == -1) {
            return -1;
        }
        int rightVal = recur(node.right);
        if (rightVal == -1) {
            return -1;
        }
        if (Math.abs(leftVal - rightVal) > 1) {
            return -1;
        }
        return Math.max(leftVal, rightVal) + 1;
    }
}
